﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.BLL;
using System.Data.SqlClient;
using SeDuce.DAL;

public partial class Management_Orders_List : PagerBase
{
    Lazy_Yu<OrderBLL> lazyOrderBLL = new Lazy_Yu<OrderBLL>(() => new OrderBLL());
    public OrderBLL LazyOrderBLL
    {
        get { return this.lazyOrderBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.BindGridView();
        
    }

    private void BindGridView()
    {
        if (string.IsNullOrEmpty(this.GetAttributes("Search_Order")))
        {
            this.RegistrPager(this.GridView1, this.rptPageFliper, 7, this.FillPageMoudle(20, SeDuce.DAL.OrderDAL.sqlGetOrders, new List<SortField>() { 
                new SortField() { FieldName = "Date", DESC = true }
            }));
        }
        else
        {
            SqlParameter[] sp = new SqlParameter[] { 
                new SqlParameter ("@OrderID", this.GetAttributes("Search_Order"))
            };
            this.RegistrPager(this.GridView1, this.rptPageFliper, 7, this.FillPageMoudleWithParams(20, string.Format(SeDuce.DAL.OrderDAL.sqlGetOrderByOrderID, "@OrderID"), sp, new List<SortField>() { 
                new SortField() { FieldName = "Date", DESC = true }
            }));
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow row = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
        string strID = ((Label)this.GridView1.Rows[row.RowIndex].Cells[1].FindControl("lblID")).Text.Trim();
        
        if (e.CommandName == "DEL")
        {
            this.LazyOrderBLL.DeleteOrder(strID);
            this.BindPage(this.rptPageFliper.UniqueID, 1);
        }
    }
    protected void lbtnSearch_Click(object sender, EventArgs e)
    {
        this.AddAttributes("Search_Order", this.txtOrderID.Text.Trim());
        this.BindGridView();
        this.BindPage(this.rptPageFliper.UniqueID, 1);
    }
    protected void lbtnTop10_Click(object sender, EventArgs e)
    {
        Response.Redirect("Top10.aspx");
    }
}